<template>
  <div class="modal-overlay" @click.self="$emit('close')">
    <div class="login-modal">
      <div class="modal-header">
        <h2>用户登录</h2>
        <p class="subtitle">欢迎登录慧医数字医疗应用系统</p>
      </div>
      
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="input-group">
          <label for="account">账号</label>
          <div class="input-wrapper">
            <span class="input-icon">👤</span>
            <input type="text" id="account" v-model="account" required placeholder="请输入您的账号" />
          </div>
        </div>
        <div class="input-group">
          <label for="password">密码</label>
          <div class="input-wrapper">
            <span class="input-icon">🔒</span>
            <input type="password" id="password" v-model="password" required placeholder="请输入您的密码" />
          </div>
        </div>
        <button type="submit" class="login-btn" :disabled="loading">
          <span v-if="loading" class="btn-icon">⏳</span>
          <span v-else class="btn-icon">🔐</span>
          <span>{{ loading ? '登录中...' : '立即登录' }}</span>
        </button>
      </form>
      
      <div v-if="errorMsg" class="login-result" :class="{ 'success': !errorMsg.includes('失败') && !errorMsg.includes('错误') }">
        {{ errorMsg }}
      </div>
      
      <div class="modal-footer">
        <button class="close-button" @click="$emit('close')">
          <span class="close-icon">✕</span>
          <span class="close-text">关闭</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'

const emit = defineEmits(['close', 'login-success'])

const account = ref('')
const password = ref('')
const loading = ref(false)
const errorMsg = ref('')

const handleLogin = async () => {
  if (!account.value || !password.value) {
    errorMsg.value = '账号和密码不能为空！'
    return
  }
  loading.value = true
  errorMsg.value = ''
  try {
    const res = await axios.post('http://47.93.134.56:8800/user/login', {
      account: account.value,
      password: password.value
    })
    if (res.data.code === 200) {
      // 保存 token 和用户信息
      localStorage.setItem('token', res.data.data.token)
      // 设置 axios 默认 header 包含 token
      axios.defaults.headers.common['token'] = `${res.data.data.token}`
      // 显示成功信息
      errorMsg.value = res.data.msg || '登录成功'
      // 延迟跳转，让用户看到成功信息
      setTimeout(() => {
        emit('login-success', res.data.data.account, password.value, res.data.data.role, res.data.data.name)
      }, 200)
    } else {
      errorMsg.value = res.data.msg || '登录失败，请检查账号密码'
    }
  } catch (e) {
    errorMsg.value = e.response?.data?.msg || '网络错误或服务器异常'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(5px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.login-modal {
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  width: 100%;
  max-width: 450px;
  position: relative;
  overflow: hidden;
  animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.modal-header {
  padding: 40px 40px 20px;
  text-align: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.modal-header h2 {
  font-size: 2rem;
  font-weight: 700;
  margin: 0 0 10px 0;
  letter-spacing: 0.5px;
}

.subtitle {
  font-size: 1rem;
  opacity: 0.9;
  margin: 0;
  font-weight: 400;
}

.login-form {
  padding: 30px 40px;
}

.input-group {
  margin-bottom: 25px;
  text-align: left;
}

.input-group label {
  display: block;
  font-size: 0.95rem;
  color: #333;
  margin-bottom: 8px;
  font-weight: 600;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 15px;
  font-size: 1.1rem;
  color: #666;
  z-index: 1;
}

.input-group input {
  width: 100%;
  padding: 15px 15px 15px 45px;
  font-size: 1rem;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  box-sizing: border-box;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

.input-group input:focus {
  outline: none;
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
}

.input-group input::placeholder {
  color: #999;
}

.login-btn {
  width: 100%;
  padding: 15px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
  margin-top: 10px;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

.login-btn:active {
  transform: translateY(0);
}

.btn-icon {
  font-size: 1.2rem;
}

.modal-footer {
  padding: 20px 40px 30px;
  text-align: center;
  border-top: 1px solid #f0f0f0;
  background: #f8f9fa;
}

.close-button {
  background: none;
  border: 2px solid #ddd;
  color: #666;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.close-button:hover {
  border-color: #999;
  color: #333;
  background: #f0f0f0;
}

.close-icon {
  font-size: 1rem;
  font-weight: bold;
}

.close-text {
  font-weight: 500;
}

/* 登录结果信息样式 */
.login-result {
  margin: 15px 40px 0;
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  text-align: center;
  background: #fee;
  color: #c53030;
  border: 1px solid #feb2b2;
}

.login-result.success {
  background: #f0fff4;
  color: #2f855a;
  border: 1px solid #9ae6b4;
}

/* 禁用状态的登录按钮 */
.login-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
  transform: none;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-modal {
    margin: 20px;
    max-width: none;
  }
  
  .modal-header,
  .login-form,
  .modal-footer {
    padding: 30px 25px;
  }
  
  .modal-header h2 {
    font-size: 1.8rem;
  }
  
  .subtitle {
    font-size: 0.9rem;
  }
}
</style> 